﻿using FTBS.Models.Entities;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;

namespace FTBS.Models.DAO
{
    public class FormatDAO
    {
        private FilmTicketBookingSystemEntities _entities = new FilmTicketBookingSystemEntities();

        /// <summary>
        /// Load all Formats
        /// </summary>
        /// <returns>List of Formats</returns>
        /// <author>VuongVT</author>
        public List<Format> LoadFormats()
        {
            try
            {
                List<Format> listFormat = new List<Format>();
                var formats = from format in _entities.Formats
                              where format.IsDeleted == false
                              orderby format.FormatId descending
                              select format;
                if (formats != null)
                {
                    listFormat = formats.ToList();
                }
                return listFormat;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Load Format By Id
        /// </summary>
        /// <returns>Format</returns>
        /// <author>VuongVT</author>
        public Format LoadFormatsById(int id)
        {
            try
            {
                Format format = new Format();
                format = (from formatEnt in _entities.Formats
                          where formatEnt.FormatId == id
                             && formatEnt.IsDeleted == false
                          select formatEnt).FirstOrDefault();
                return format;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Add a film
        /// </summary>
        /// <returns>1: Success -- 0: Fail</returns>
        /// <author>VuongVT</author>
        public int Add(Format format)
        {
            try
            {
                _entities.Formats.Add(format);
                int resultQuery = _entities.SaveChanges();
                return resultQuery;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Edit a Film
        /// </summary>
        /// <returns>1: Success -- 0: Fail</returns>
        /// <author>VuongVT</author>
        public int Edit(Format format)
        {
            try
            {
                _entities.Entry(format).State = EntityState.Modified;
                int resultQuery = _entities.SaveChanges();
                return resultQuery;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Delete a film
        /// </summary>
        /// <returns>1: Success -- 0: Fail</returns>
        /// <author>VuongVT</author>
        public int Delete(Format format)
        {
            try
            {
                format.IsDeleted = true;
                _entities.Entry(format).State = EntityState.Modified;
                int result = _entities.SaveChanges();
                return result;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Check if format name is existed or not
        /// </summary>
        /// <param name="format"></param>
        /// <returns>true || false</returns>
        /// <author>hungph</author>
        /// <date>3/3/2014</date>
        public bool CheckIsExisted(Format format)
        {
            try
            {
                var isExist = _entities.Formats.Any(c => c.FormatName == format.FormatName && c.IsDeleted == false);
                return isExist;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public Format GetFormatById(int id)
        {
            var format = _entities.Formats.Find(id);
            return format;
        }
    }
}